<?php

class GoodsTypeController extends Controller {

    public function indexAction() {
        $goodTypeModel = Factory::M("GoodsTypeModel");
        $current = isset($_GET['page']) ? $_GET['page'] : 1;
        $pagesize = 10;
        $offset = ($current - 1) * $pagesize;
        $types = $goodTypeModel->getGoodsTypesPage($offset, $pagesize);
        $total = $goodTypeModel->total("");
        $page = new Page($total, $pagesize, $current, 'index.php',
            array('p' => 'back', 'c' => 'goodstype', 'a' => 'index'));
        $pageinfo = $page->showPage();
        include CURRENT_VIEW_PATH . 'goods_type_list.html';
    }

    public function addAction() {
        include CURRENT_VIEW_PATH . 'goods_type_add.html';
    }

    public function insertAction() {
        $data = array();
        $data['type_name'] = $_POST['type_name'];

        if(empty($data['type_name'])) {
            $this->_jump("index.php?p=back&c=goodstype&a=add", "商品类型名称不能为空", 1);
        }

        $goodTypeModel = Factory::M("GoodsTypeModel");
        if($goodTypeModel->insert($data)) {
            $this->_jump("index.php?p=back&c=goodstype&a=index");
        }
        else {
            $this->_jump("index.php?p=back&c=goodstype&a=add", "添加商品类型失败");
        }

    }


}